import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:zywz/config/app_colors.dart';
import 'package:zywz/utils/common_ui.dart';

import '../models/user_company.dart';

// 企业列表项组件
class EnterpriseItem extends StatelessWidget {
  final String commidityId;
  final String enterpriseName;
  final bool isCurrent;
  final VoidCallback onSwitch;
  final VoidCallback onDelete; // 添加删除回调
  const EnterpriseItem({
    Key? key,
    required this.commidityId,
    required this.enterpriseName,
    required this.isCurrent,
    required this.onSwitch,
    required this.onDelete,
  }) : super(key: key);


  @override
  Widget build(BuildContext context) {



    return Slidable(
        endActionPane: ActionPane(
          motion: const BehindMotion(),
          children: [
            SlidableAction(
              onPressed: (context){
                if(isCurrent){
                  EasyLoading.showError('当前企业不能删除');
                  Slidable.of(context)?.close();
                  return;
                }
                onDelete();
              },
              backgroundColor: Color(0xFFFE4A49),
              foregroundColor: Colors.white,
              icon: Icons.delete,
              label: '删除',
              spacing: 0,
              flex:12,
            )
          ],
        ),
        child: Container(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(color: const Color(0xFFF0F0F0), width: 1.0),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // 企业名称和当前标识
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                enterpriseName,
                style: TextStyle(
                  fontSize: 16.0,
                  color: const Color(0xFF333333),
                  fontWeight: isCurrent ? FontWeight.bold : FontWeight.normal,
                ),
              ),
            ],
          ),

          // 切换企业按钮
          TextButton(
            onPressed: onSwitch,
            style: TextButton.styleFrom(
              foregroundColor: const Color(0xFF41C7DD),
              padding: EdgeInsets.zero,
            ),
            child:isCurrent? const SizedBox(
              child: Text(
                '当前所在',
                style: TextStyle(
                  fontSize: 16.0,
                  color: Colors.orange,
                ),
              ),
            ): const Text(
              '切换企业',
              style: TextStyle(
                fontSize: 16.0,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    )
    );

  }
}

// 企业列表组件
class EnterpriseListView extends StatelessWidget {
  final List<AppCompanyDto> enterprises;
  final String currentEnterprise;
  final ValueChanged<AppCompanyDto> onEnterpriseSelected;
  final ValueChanged<AppCompanyDto> onEnterpriseDeleted;

  const EnterpriseListView({
    Key? key,
    required this.enterprises,
    required this.currentEnterprise,
    required this.onEnterpriseSelected,
    required this.onEnterpriseDeleted,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: enterprises.length,
      itemBuilder: (context, index) {
        final enterprise = enterprises[index];
        return EnterpriseItem(
          commidityId: enterprise?.id ?? '',
          enterpriseName: enterprise?.name ?? '',
          isCurrent: enterprise?.id == currentEnterprise,
          onSwitch: () => onEnterpriseSelected(enterprise),
          onDelete: () => onEnterpriseDeleted(enterprise),
        );
      },
    );
  }
}